﻿<%@ WebHandler Language="C#" Class="AddToCart" %>

using System;
using System.Web;
using System.Collections.Generic;

public class AddToCart : FiveSistersHandler {
    
    public override void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        if (IsAuthenticate(context))
        {
            try
            {
                int pi = 0;
                if (context.Request.QueryString["pi"] != null)
                    pi = int.Parse(context.Request.QueryString["pi"].ToString());

                Product product = new Product().Detail(pi);
                if (product != null)
                {
                    OrderDetail detail = new OrderDetail();
                    detail.ProductId = product.ProductId;
                    detail.Product = product;
                    detail.Quantity = 1;
                    detail.PriceAtBuy = product.ProductPrice;
                    Order order = context.Session[FiveSistersCore.Cart] as Order;
                    if (order.OrderDetails == null) order.OrderDetails = new List<OrderDetail>();
                    order.AddOrderDetail(detail);
                    
                    //tinh sum + price
                    int sumitem = order.TotalQuantiy();
                    int sumprice = order.TotalPrice();
                    
                    context.Response.Write("{ \"totalitem\": " + sumitem.ToString() + ", \"totalprice\": " + Formatter.PriceVietNamese(sumprice) + " }");
                }
                else
                {
                    context.Response.Write("{ \"note\": \"NONEPRODUCT\" }");
                }
            }
            catch (Exception ex)
            {
                PMSA.Framework.Log.CLogManager.Write("AddToCart.ashx", ex.ToString(), "FIVE");
                context.Response.Write("FAIL");
            }
        }        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}